import Vue from 'vue'
import Vuex from 'vuex'
// import { stat } from 'fs'
// import axios from 'axios'

Vue.use(Vuex)
const state = {
  count: 0,
  my_info: {
    nickname: '',
    avatar: '',
    id: ''
  },
  left_panel_type: '',
  right_panel_type: '',
  current_chatting_id: '',
  friendlist: {},
  single_chat_record: {}
}
const getters = {
  getMyInfo (state) {
    return state.my_info
  },
  getRightPanelType (state) {
    return state.right_panel_type
  },
  getLeftPanelTitle (state) {
    switch (state.left_panel_type) {
      case 'history': return '最近聊天'
      case 'friend': return '好友'
      case 'group': return '群组'
      case 'circle': return '朋友圈'
      case 'my': return '我的'
      default: return ''
    }
  },
  getCurrentChattingId (state) {
    return state.current_chatting_id
  },
  getUserInfoByCurrentId (state) { // 通过id获取用户信息
    return state.friendlist[state.current_chatting_id]
  },
  getSingleChatRecordByCurrentId (state) { // 通过id获取聊天记录
    return state.single_chat_record[state.current_chatting_id]
  },
  getFriendList (state) {
    return state.friendlist
  }
}
const mutations = {
  add (state, n = 0) {
    return state.count + n
  },
  setMyUserInfo (state, userinfo) {
    state.my_info.nickname = userinfo.nickname
    state.my_info.avatar = userinfo.avatar
    state.my_info.id = userinfo.id
    state.friendlist = userinfo.friendlist
  },
  setMyChatRecord (state, chatdata) {
    state.single_chat_record = chatdata
  },
  setRightPanelType (state, type) {
    state.right_panel_type = type
  },
  setLeftPanelType (state, type) {
    state.left_panel_type = type
  },
  setCurrentChattingId (state, id) {
    state.current_chatting_id = id
  },
  modifyMyNickname (state, nickname) {
    state.my_info.nickname = nickname
  }
}
const actions = {
  // setLeftPage (context, pagename) {
  //   context.commit('setLeftPage', pagename)
  // },
  // getFriendList (userid) {
  //   axios.post('localhost:8080/getFriendList').then().catch((err) => {
  //     console.log(err)
  //   })
  // }
}

export default new Vuex.Store({
  state,
  getters,
  mutations,
  actions
})
